package com.invengo.testrfid.app.utils;

import android.os.Environment;

import com.invengo.testrfid.app.dao.RfidCageBind;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExportExcelData {

    public void exportExcelData(List<RfidCageBind> list) throws IOException, WriteException {

        String s = Environment.getExternalStorageDirectory()
                .toString() + "/invengo";

        File file = new File(s);
        if (!file.exists()) {
            file.mkdir();
        }
        WritableWorkbook book = Workbook
                .createWorkbook(new File(s + "/" + TimeUtilsNew.getTimebyLong(System.currentTimeMillis(), "yyMMddHHmm")
                        + "-Data.xls"));
        WritableSheet sheet = book.createSheet("工作表1", 0);


        Label label_00 = new Label(0, 0, "集装箱编号");
        Label label_10 = new Label(1, 0, "Tid_1");
        Label label_20 = new Label(2, 0, "Tid_2");
        Label label_30 = new Label(3, 0, "绑定时间");

        /**
         * 注意：要把所有单元格对象添加到工作表中
         */
        sheet.addCell(label_00);
        sheet.addCell(label_10);
        sheet.addCell(label_20);
        sheet.addCell(label_30);

        for (int i = 0; i < list.size(); i++) {

            RfidCageBind rfidCageBind = list.get(i);

            Label label_00_a = new Label(0, i + 1, rfidCageBind.getCageId());

            String[] split = rfidCageBind.getTagId().split(Constant.ValueOfSpare);
            Label label_10_b = new Label(1, i + 1, split[0]);
            Label label_20_c = new Label(2, i + 1, split[1]);
            Label label_30_c = new Label(3, i + 1, rfidCageBind.getBindTime());

            sheet.addCell(label_00_a);
            sheet.addCell(label_10_b);
            sheet.addCell(label_20_c);
            sheet.addCell(label_30_c);
        }

        /*
         * 写入到文件
         */
        book.write();
        /*
         * 一定要使用close()方法来关闭先前创建的对象，以释放其所占用的内存空间
         */
        book.close();


    }
}
